package client.comm.commlib.base

import android.os.Build
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.view.marginLeft
import androidx.recyclerview.widget.RecyclerView
import com.youth.banner.adapter.BannerAdapter
import android.widget.RelativeLayout
import client.comm.commlib.R
import client.comm.commlib.dipToPx
import com.youth.banner.util.BannerUtils


abstract class SyBannerAdapter<T>(val datas: List<T>) :
    BannerAdapter<T, SyBannerAdapter.BannerViewHolder>(datas) {

    class BannerViewHolder(val imageView: ImageView) : RecyclerView.ViewHolder(imageView)

    override fun onCreateHolder(parent: ViewGroup?, viewType: Int): BannerViewHolder {
        val imageView = ImageView(parent?.getContext())
        //注意，必须设置为match_parent，这个是viewpager2强制要求的
        imageView.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        val margin = ViewGroup.MarginLayoutParams(imageView.layoutParams).apply {
            leftMargin = dipToPx(imageView.resources, 10f).toInt()
            rightMargin = dipToPx(imageView.resources, 10f).toInt()
        }
        val layoutParams = RelativeLayout.LayoutParams(margin)
        imageView.layoutParams = layoutParams

        //通过裁剪实现圆角
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            BannerUtils.setBannerRound(imageView, 20f)
        }
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        return BannerViewHolder(imageView)
    }
}